package com.application.usertable.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.mac.entity.Mac;
import com.application.mac.service.MacService;
import com.application.mac.struts.form.MacForm;
import com.application.role.entity.Role;
import com.application.usertable.commons.Constants;
import com.application.usertable.entity.RoleUser;
import com.application.usertable.entity.Usertable;
import com.application.usertable.service.UsertableService;
import com.application.usertable.struts.form.UsertableForm;
import com.application.yfsyslog.entity.YfSyslog;
import com.application.yfsyslog.service.YfSyslogService;
import com.manager.commons.CommonUtil;
import com.manager.commons.RandomKey;
import com.manager.commons.StringUtil;
import com.manager.commons.page.PageBean;
import com.manager.mail.MailUtil;
import com.util.ApplicationUtil;

public class UsertableAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private UsertableService usertableService;

	private MacService macService;

	private YfSyslogService yfSyslogService;
	
	private String ids;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());
	StringUtil strUtil = new StringUtil();

	/**
	 * 实体对象
	 */
	@SuppressWarnings("unchecked")
	public ActionForward userLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("searchUsertable" + "开始");
		String str = "flase";
		HttpSession session = request.getSession();
		UsertableForm usertable = (UsertableForm) form;
		usertable.setTemp("login");
		String macurl = request.getParameter("mac");
		String message = "登录失败！";
		String rand = (String) session.getAttribute("rand");
		String code = request.getParameter("code");

		ServletContext sc = request.getSession().getServletContext();
		// WebApplicationContext ctx =
		// (WebApplicationContext)sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
		ApplicationUtil au = new ApplicationUtil();
		if (au.getSys("1").equals("open")) {
			if (request.getParameter("code") != null && !code.equals("")
					&& rand.equals(code)) {
				if (macurl != null && !macurl.equals("")) {
					if (usertable.getUsername() != null
							&& usertable.getPassword() != null
							&& !usertable.getUsername().equals("")
							&& !usertable.getPassword().equals("")) {
						MacForm mac = new MacForm();
						List usertableList = null;
						List macList = null;
						mac.setMac(macurl);
						usertable.setPassword(strUtil.MD5Encode(usertable
								.getPassword()));
						usertableList = usertableService
								.searchUsertable(usertable);
						macList = macService.searchMac(mac);
						if (usertableList != null && usertableList.size() > 0) {
							if (!macurl.equals("") && macList != null
									&& macList.size() > 0) {
								str = Constants.SUCCESS;
								Mac mactemp = new Mac();
								Usertable usertable1 = (Usertable) usertableList
										.get(0);
								List<Role> roles = au.getUserRole(usertable1
										.getId());
								usertable1.setRoleList(roles,usertable1.getUsername());
								usertable1.setRole(roles.get(0).getName());
								session.setAttribute("loginUser", usertable1);
								mactemp = (Mac)macList.get(0);
								mac.setMac(mactemp.getMac());
								mac.setSpecial(mactemp.getSpecial());
								session.setAttribute("mac", mac);
							
								logLogin(usertable1, macurl, request);
							} else {
								message = "网卡MAC地址不在系统库中，该机器无法登陆！";
							}
						} else {
							message = "用户名或密码有误，无法登陆！";
						}
					}
				} else {
					message = "没有获取MAC，请检查浏览器安全级别设置！";
				}
			} else {
				message = "请填写正确的验证码！";
			}
		} else {
			if (request.getParameter("code") != null && !code.equals("")
					&& rand.equals(code)) {
				if (usertable.getUsername() != null
						&& usertable.getPassword() != null
						&& !usertable.getUsername().equals("")
						&& !usertable.getPassword().equals("")) {
					List usertableList = null;
					 usertable.setPassword(strUtil.MD5Encode(usertable.getPassword()));
					usertableList = usertableService.searchUsertable(usertable);
					if (usertableList != null && usertableList.size() > 0) {
						str = Constants.SUCCESS;
						Usertable usertable1 = (Usertable) usertableList.get(0);
						List<Role> roles = au.getUserRole(usertable1.getId());
						usertable1.setRoleList(roles,usertable1.getUsername());
						if(roles!=null&&roles.size()>0){
							usertable1.setRole(roles.get(0).getName());
						}
				
						session.setAttribute("loginUser", usertable1);
						logLogin(usertable1, "", request);

					} else {
						message = "用户名或密码有误，无法登陆！";
					}
				}
			} else {
				message = "请填写正确的验证码！";
			}
		}
		log.debug("searchUsertable" + "结束");
		request.setAttribute("message", message);
		return mapping.findForward(str);
	}

	private void logLogin(Usertable user, String mac, HttpServletRequest request) {
		if(!"sys".equals(user.getUsername())){
			Date date = new Date();
			YfSyslog log = new YfSyslog();
			log.setId(RandomKey.generateID_Long_19());
			log.setUserId(user.getId());
			log.setOrg(user.getOrg());
			log.setLoginTime(date);
			log.setCreateDate(date);
			log.setMac(mac);
			log.setIp(getIpAddr(request));
			log.setOperateid(user.getId());
			yfSyslogService.insertObject(log);
		}
	}

	private Usertable usertable;

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getUsertablePartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchUsertable" + "开始");
		UsertableForm usertable = (UsertableForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List usertableList = null;
		resultSize = usertableService.getAllCountUsertable(usertable);
		pageBean = PageBean.getPageBean("usertableTab", resultSize, request);
		usertableList = usertableService.searchUsertable(usertable, pageBean);
		request.setAttribute("usertableList", usertableList);

		log.debug("searchUsertable" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddUsertable(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddUsertables");
		request.setAttribute("yearSelect", CommonUtil.getYear());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addUsertable(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addUsertables");
		Usertable usertable = this.getUsertable(form);
		String id = RandomKey.generateID_Long_19().toString();
		String roleId = request.getParameter("roleId");
		String roleName = request.getParameter("roleName");
		String[] roleIds = null;
		if (roleId != null) {
			roleIds = roleId.split(",");
		}
		String[] zc = request.getParameterValues("specialty");
		String specialty = "";
		if(zc!=null){
			for (int i = 0; i < zc.length; i++) {
				specialty = zc[i] + " " + specialty;
			}
		}
		usertable.setSpecialty(specialty);
		usertable.setId(id);
		usertable.setCreateDate(new Date());
		usertable.setPassword(strUtil.MD5Encode(usertable.getPassword()));
		this.usertableService.insertObject(usertable);

		if (roleIds != null) {
			for (int i = 0; i < roleIds.length; i++) {
				if (!roleIds[i].equals("")) {
					RoleUser r = new RoleUser();
					r.setId(RandomKey.generateID_Long_19().toString());
					r.setUid(id);
					r.setRid(roleIds[i]);
					r.setCreateDate(new Date());
					this.usertableService.insertObject(r);
				}
			}
		}
		 List<Usertable> listuser = this.usertableService.getUserById("2012022022320903323","2012022022120511934");
		 List<Usertable> listuser2 = this.usertableService.getUserById("2012021517543028685","2012022022120511934");
		 if(listuser!=null&&listuser.size()>0){
			 SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
			  String a1=dateformat1.format(new Date());
			 String title = "新建用户提醒";
			 MailUtil mu = new MailUtil();
			 Usertable ut = listuser.get(0);
			 Usertable ut2 = listuser2.get(0);
				if (ut.getEMAIL() != null
				&& !ut.getEMAIL().equals("")) {
			String content = "研发管理系统中，系统管理员【"+ut2.getRname()+"】于"+a1+" " +
					"在系统中新增了一个用户（用户名："+usertable.getRname()+"、登录名："+usertable.getUsername()+"，" +
							"角色是【"+roleName+"】）。该消息由研发管理系统自动发出！";
			mu.sendEmail(title, content, ut.getEMAIL());
		}
		 }

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteUsertable(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteUsertables");
		UsertableForm usertableForm = (UsertableForm) form;
		this.usertableService.deleteUsertables(usertableForm);
		this.usertableService.deleteUserRole(usertableForm.getId());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateUsertable(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateUsertables");
		UsertableForm usertableForm = (UsertableForm) form;
		usertable = (Usertable) this.usertableService.searchUsertable(
				usertableForm).get(0);
		List<Role> roleUser = this.usertableService.getUserRole(usertableForm
				.getId());
		String roleIds = "";
		String roleNames = "";
		if (roleUser != null && roleUser.size() > 0) {
			for (int i = 0; i < roleUser.size(); i++) {
				Role temp = roleUser.get(i);
				roleIds = temp.getId() + "," + roleIds;
				roleNames = temp.getName() + " " + roleNames;
			}

		}
		request.setAttribute("yearSelect", CommonUtil.getYear());
		request.setAttribute(Constants.ROLEIDS, roleIds);
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, usertable);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateUsertable(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateUsertables");
		Usertable usertable = this.getUsertable(form);
		String[] zc = request.getParameterValues("specialty");
		String specialty = "";
		if(zc!=null){
			for (int i = 0; i < zc.length; i++) {
				specialty = zc[i] + " " + specialty;
			}
		}
		usertable.setSpecialty(specialty);
		usertable.setId(usertable.getId());
		usertable.setCreateDate(new Date());
		if(request.getParameter("passtype")!=null&&request.getParameter("passtype").equals("add")){
			usertable.setPassword(strUtil.MD5Encode(usertable.getPassword()));
		}
		this.usertableService.updateObject(usertable);
		this.usertableService.deleteUserRole(usertable.getId());
		String roleId = request.getParameter("roleId");
		String[] roleIds = null;
		if (roleId != null) {
			roleIds = roleId.split(",");
		}
		if (roleIds != null) {
			for (int i = 0; i < roleIds.length; i++) {
				if (!roleIds[i].equals("")) {
					RoleUser r = new RoleUser();
					r.setId(RandomKey.generateID_Long_19().toString());
					r.setUid(usertable.getId());
					r.setRid(roleIds[i]);
					r.setCreateDate(new Date());
					this.usertableService.insertObject(r);
				}
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewUsertable(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewUsertable");
		UsertableForm usertableForm = (UsertableForm) form;
		usertable = (Usertable) this.usertableService.searchUsertable(
				usertableForm).get(0);
		List<Role> roleUser = this.usertableService.getUserRole(usertableForm
				.getId());
		String roleNames = "";
		if (roleUser != null && roleUser.size() > 0) {
			for (int i = 0; i < roleUser.size(); i++) {
				Role temp = roleUser.get(i);
				roleNames = temp.getName() + " " + roleNames;
			}

		}
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, usertable);
		return mapping.findForward(Constants.SUCCESS);
	}

	@SuppressWarnings("unchecked")
	public ActionForward getEmployeePartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchUsertable" + "开始");
		UsertableForm usertable = (UsertableForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List usertableList = null;
	
		resultSize = usertableService.getAllCountUsertable(usertable);
		pageBean = PageBean.getPageBean("usertableTab", resultSize, request);
		usertableList = usertableService.searchUsertable(usertable, pageBean);
		request.setAttribute("usertableList", usertableList);

		log.debug("searchUsertable" + "结束");

		return mapping.findForward(Constants.SUCCESSEM);
	}
  
	public ActionForward popuSelectEmployee(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("popuSelectEmployee" + "开始");
		UsertableForm usertable = (UsertableForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List usertableList = null;
		ids = usertable.getIds();
		
		
		
		
		if(StringUtils.isNotBlank(ids)){
//			String queryIds = "";
//			String[] tmp = ids.split(",");
//			for (int i = 0; i < tmp.length; i++) {
//				if(i==0){
//					queryIds = "'" + tmp[i] + "'";
//				}else{
//					queryIds = queryIds + "," + "'" + tmp[i] + "'";
//				}
//				
//			}

			if(ids.startsWith(",")){
				ids = ids.substring(1);
			}
			if(ids.endsWith(",")){
				ids = ids.substring(0,ids.length()-1);
			}
			
			usertable.setIds(ids);
		}
		resultSize = usertableService.getAllCountUsertable(usertable);
		pageBean = PageBean.getPageBean("usertableTab", resultSize, request);
		usertableList = usertableService.searchUsertable(usertable, pageBean);
		request.setAttribute("usertableList", usertableList);
		log.debug("popuSelectEmployee" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddUsertables");
		request.setAttribute("yearSelect", CommonUtil.getYear());
		return mapping.findForward(Constants.SUCCESSEM);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addUsertables");
		Usertable usertable = this.getUsertable(form);
		String[] zc = request.getParameterValues("specialty");
		String specialty = "";
		if(zc!=null){
			for (int i = 0; i < zc.length; i++) {
				specialty = zc[i] + " " + specialty;
			}
		}
		usertable.setSpecialty(specialty);
		String id = RandomKey.generateID_Long_19().toString();
		String roleId = request.getParameter("roleId");
		String[] roleIds = null;
		if (roleId != null) {
			roleIds = roleId.split(",");
		}
		usertable.setId(id);
		usertable.setCreateDate(new Date());

		this.usertableService.insertObject(usertable);

		if (roleIds != null) {
			for (int i = 0; i < roleIds.length; i++) {
				if (!roleIds[i].equals("")) {
					RoleUser r = new RoleUser();
					r.setId(RandomKey.generateID_Long_19().toString());
					r.setUid(id);
					r.setRid(roleIds[i]);
					r.setCreateDate(new Date());
					this.usertableService.insertObject(r);
				}
			}
		}
		return mapping.findForward(Constants.SUCCESSEM);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteUsertables");
		UsertableForm usertableForm = (UsertableForm) form;
		this.usertableService.deleteUsertables(usertableForm);
		this.usertableService.deleteUserRole(usertableForm.getId());
		return mapping.findForward(Constants.SUCCESSEM);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateEmployee(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateUsertables");
		UsertableForm usertableForm = (UsertableForm) form;
		usertable = (Usertable) this.usertableService.searchUsertable(
				usertableForm).get(0);
		List<Role> roleUser = this.usertableService.getUserRole(usertableForm
				.getId());
		String roleIds = "";
		String roleNames = "";
		if (roleUser != null && roleUser.size() > 0) {
			for (int i = 0; i < roleUser.size(); i++) {
				Role temp = roleUser.get(i);
				roleIds = temp.getId() + "," + roleIds;
				roleNames = temp.getName() + " " + roleNames;
			}

		}
		request.setAttribute("yearSelect", CommonUtil.getYear());
		request.setAttribute(Constants.ROLEIDS, roleIds);
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, usertable);
		return mapping.findForward(Constants.SUCCESSEM);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateUsertables");
		Usertable usertable = this.getUsertable(form);
		String[] zc = request.getParameterValues("specialty");
		String specialty = "";
		if(zc!=null){
			for (int i = 0; i < zc.length; i++) {
				specialty = zc[i] + " " + specialty;
			}
		}
		usertable.setSpecialty(specialty);
		usertable.setCreateDate(new Date());
		this.usertableService.updateObject(usertable);
//		this.usertableService.deleteUserRole(usertable.getId());
//		String roleId = request.getParameter("roleId");
//		String[] roleIds = null;
//		if (roleId != null) {
//			roleIds = roleId.split(",");
//		}
//		if (roleIds != null) {
//			for (int i = 0; i < roleIds.length; i++) {
//				if (!roleIds[i].equals("")) {
//					RoleUser r = new RoleUser();
//					r.setId(RandomKey.generateID_Long_19().toString());
//					r.setUid(usertable.getId());
//					r.setRid(roleIds[i]);
//					r.setCreateDate(new Date());
//					this.usertableService.insertObject(r);
//				}
//			}
//		}
		return mapping.findForward(Constants.SUCCESSEM);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewUsertable");
		UsertableForm usertableForm = (UsertableForm) form;
		usertable = (Usertable) this.usertableService.searchUsertable(
				usertableForm).get(0);
		List<Role> roleUser = this.usertableService.getUserRole(usertableForm
				.getId());
		String roleNames = "";
		if (roleUser != null && roleUser.size() > 0) {
			for (int i = 0; i < roleUser.size(); i++) {
				Role temp = roleUser.get(i);
				roleNames = temp.getName() + " " + roleNames;
			}

		}
		request.setAttribute(Constants.ROLENAMES, roleNames);
		request.setAttribute(Constants.DEPARTMENT, usertable);
		return mapping.findForward(Constants.SUCCESSEM);
	}

	// get--set
	/**
	 * @return the UsertableService
	 */
	public UsertableService getUsertableService() {
		return this.usertableService;
	}

	/**
	 * @param UsertableService
	 *            the UsertableService to set
	 */
	public void setUsertableService(UsertableService usertableService) {
		this.usertableService = usertableService;
	}

	/**
	 * 获取Usertable实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Usertable
	 */
	public Usertable getUsertable(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		UsertableForm usertableForm = (UsertableForm) form;
		usertable = new Usertable();
		BeanUtils.copyProperties(usertable, usertableForm);
		return usertable;
	}

	public void setUsertable(Usertable usertable) {
		this.usertable = usertable;
	}

	/**
	 * @return the macService
	 */
	public MacService getMacService() {
		return macService;
	}

	/**
	 * @param macService
	 *            the macService to set
	 */
	public void setMacService(MacService macService) {
		this.macService = macService;
	}

	/**
	 * @return the usertable
	 */
	public Usertable getUsertable() {
		return usertable;
	}

	public ActionForward resetPassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		UsertableForm usertableForm = (UsertableForm) form;
		String[] ids = usertableForm.getIds().split(",");
		String title = "密码重置提醒";

		MailUtil mu = new MailUtil();
		for (int i = 0; i < ids.length; i++) {
			usertableForm.setId(ids[i]);
			String passTemp = StringUtil.getRandomNun();
			usertable = (Usertable) this.usertableService.searchUsertable(
					usertableForm).get(0);
			 usertable.setPassword(strUtil.MD5Encode(passTemp));
//			usertable.setPassword(passTemp);
			usertableService.updateObject(usertable);
			if (usertable.getEMAIL() != null
					&& !usertable.getEMAIL().equals("")) {
				String content = usertable.getRname() + "，您好：\n 您在研发管理系统中的密码已经由系统管理员重置，现在的密码是"
						+ passTemp + "，这个密码是系统自动生成的，请及时修改密码，并妥善保存！";
				mu.sendEmail(title, content, usertable.getEMAIL());
			}
		}

		return mapping.findForward("resetPassword");
	}

	public ActionForward updatePassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		UsertableForm usertableForm = (UsertableForm) form;
		String title = "密码修改提醒";
		HttpSession session = request.getSession();
		Usertable usertableTemp = (Usertable) session.getAttribute("loginUser");
		MailUtil mu = new MailUtil();
		String passTemp = strUtil.MD5Encode(request.getParameter("passTemp"));
//		String passTemp = request.getParameter("passTemp");
		if (usertableTemp.getPassword().equals(passTemp)) {
//			usertableTemp.setPassword(usertableForm.getPassword());
			usertableTemp.setPassword(strUtil.MD5Encode(usertableForm.getPassword()));
			usertableService.updateObject(usertableTemp);
//			if (usertableTemp.getEMAIL() != null
//					&& !usertableTemp.getEMAIL().equals("")) {
//				String content = usertableTemp.getRname() + "，您好：\n 您的已经修改，现在的密码是"
//						+ usertableTemp.getPassword() + "，请妥善保存！";
//				mu.sendEmail(title, content, usertableTemp.getEMAIL());
//			}
			return mapping.findForward(Constants.SUCCESS);
		}else{
			request.setAttribute("message", "密码修改失败，输入原密码不正确！");
			return mapping.findForward("flase");
		}
	}

	public YfSyslogService getYfSyslogService() {
		return yfSyslogService;
	}

	public void setYfSyslogService(YfSyslogService yfSyslogService) {
		this.yfSyslogService = yfSyslogService;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	// public ActionForward creatTreeSelect(ActionMapping mapping,
	// ActionForm form, HttpServletRequest request,
	// HttpServletResponse response) throws IOException {
	// response.setContentType("text/text;charset=UTF-8");
	// // System.out.println(request.getParameter("pid"));
	// UsertableForm usertableForm = new UsertableForm();
	// String vid = request.getParameter("treeid");
	// int tjs = Integer.parseInt(request.getParameter("js"));
	// Long pid = Long.parseLong(request.getParameter("pid"));
	// List<Usertable> accList = usertableService
	// .searchUsertable(usertableForm);
	// String html = creatTreeSelect(accList, vid, tjs);
	// java.io.PrintWriter out = response.getWriter();
	// out.print(html);
	// out.flush();
	// out.close();
	// return null;
	// }
	//
	// public String creatTreeSelect(List<Usertable> accList, String vid, int
	// js) {
	// String html = "";
	// for (int i = 0; i < accList.size(); i++) {
	// Usertable usertable = (Usertable) accList.get(i);
	// for (int j = 0; j < js; j++) {
	// html = html + "<img src='images/line.gif'>";
	// }
	// html = html + "<img src='images/plus.gif' id='imgt" + vid + js + i
	// + "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
	// + (js + 1) + ",'" + usertable.getId() + "');\" >"
	// + "<img src='images/close1.gif' id='img" + vid + js + i
	// + "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
	// + (js + 1) + ",'" + usertable.getId()
	// + "');\" style=\"cursor:hand\">&nbsp;";
	//
	// html = html + "<a onclick=\"selectTree('" + usertable.getId()
	// + "','" + usertable.getName()
	// + "')\" style=\"cursor:hand\">" + usertable.getName()
	// + "</a>";
	// html = html + "<div id='" + vid + js + i
	// + "' ><span class='f1'>.</span></div>";
	// }
	// return html;
	// }

}
